﻿using System;
using System.Collections.Generic;
using Dost.Khcn.Data.EntityModel;
using Dost.Khcn.Data.Implements;
using Dost.Khcn.Data.Interfaces;

namespace Dost.Khcn.Biz
{
    public class TinhThanhPhoBiz
    {
        #region Properties

        private ITinhThanhPhoDao _tinhThanhPhoDao;
        private ITinhThanhPhoDao TinhThanhPhoData
        {
            get { return _tinhThanhPhoDao ?? (_tinhThanhPhoDao = new TinhThanhPhoDao()); }
        }

        #endregion

        #region Constructors

        #endregion

        #region Methods

        public List<TinhThanhPho> GetAllTinhThanhPhos()
        {
            return TinhThanhPhoData.GetAll();
        }

        public object GetTinhThanhPhos(int startIndex, int pageSize, string sorting, string name)
        {
            try
            {
                int totalRecords;
                var tinhThanhPhos = TinhThanhPhoData.GetTinhThanhPhos(startIndex, pageSize, sorting, name, out totalRecords);
                return new { Result = "OK", Records = tinhThanhPhos, TotalRecordCount = totalRecords };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public TinhThanhPho CreateTinhThanhPho(TinhThanhPho tinhThanhPho)
        {
            // validate inputs
            if (tinhThanhPho == null) return null;

            return string.IsNullOrEmpty(tinhThanhPho.Ten) ? null : TinhThanhPhoData.AddTinhThanhPho(tinhThanhPho);
        }

        public TinhThanhPho GetById(int id)
        {
            return id > 0 ? TinhThanhPhoData.GetById(id) : null;
        }

        public void UpdateTinhThanhPho(TinhThanhPho tinhThanhPho)
        {
            if (tinhThanhPho == null) return;

            TinhThanhPhoData.UpdateTinhThanhPho(tinhThanhPho);
        }

        public object DeleteTinhThanhPho(int id)
        {
            try
            {
                TinhThanhPhoData.DeleteTinhThanhPho(id);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        #endregion
    }
}
